﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TogglLibrary.DataObjects;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Collections.Specialized;

namespace TogglLibrary
{
    public class ToggleAPI
    {
        static Uri url_prefix = new Uri("https://www.toggl.com/api/v6/");
        static Uri url_me = new Uri(url_prefix, "me.json");

        NameValueCollection authHeader;
        User loggedInUser;

        public User Login(string api_token)
        {
            authHeader = new NameValueCollection();
            authHeader.Add(
                HttpRequestHeader.Authorization.ToString(), 
                "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(api_token + ":api_token"))
            );
            var userResponse = Request(url_me);
            //var content = @"{""data"":{""api_token"":""d076ea7ba6c8e705b921c6dcaeca7e07"",""jquery_date_format"":""d.m.Y"",""jquery_timeofday_format"":""H:i"",""date_format"":""%d.%m.%Y"",""default_workspace_id"":123323,""updated_at"":""2012-02-27T00:58:55+01:00"",""fullname"":""keydon@gmail.com"",""language"":""en_US"",""id"":172321,""time_entry_retention_days"":14,""beginning_of_week"":1,""new_time_entries_start_automatically"":true,""store_start_and_stop_time"":true,""email"":""keydon@gmail.com"",""timeofday_format"":""%H:%M""},""related_data_updated_at"":""2012-03-01T20:48:53+01:00""}";
            loggedInUser = User.Parse(userResponse);
            return loggedInUser;
        }
        public User Login(string username, string password)
        {
            authHeader = new NameValueCollection();
            authHeader.Add(
                HttpRequestHeader.Authorization.ToString(),
                "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + ':' + password))
            );
            var userResponse = Request(url_me);
            //var content = @"{""data"":{""api_token"":""d076ea7ba6c8e705b921c6dcaeca7e07"",""jquery_date_format"":""d.m.Y"",""jquery_timeofday_format"":""H:i"",""date_format"":""%d.%m.%Y"",""default_workspace_id"":123323,""updated_at"":""2012-02-27T00:58:55+01:00"",""fullname"":""keydon@gmail.com"",""language"":""en_US"",""id"":172321,""time_entry_retention_days"":14,""beginning_of_week"":1,""new_time_entries_start_automatically"":true,""store_start_and_stop_time"":true,""email"":""keydon@gmail.com"",""timeofday_format"":""%H:%M""},""related_data_updated_at"":""2012-03-01T20:48:53+01:00""}";
            loggedInUser = User.Parse(userResponse);
            return loggedInUser;
        }

        private JObject Request(Uri uri)
        {
            var authRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
            authRequest.Headers.Add(authHeader);
            string content;
            /*
            var authResponse = (HttpWebResponse)authRequest.GetResponse();
            using (var reader = new StreamReader(authResponse.GetResponseStream(), Encoding.UTF8))
            {
                content = reader.ReadToEnd();
            }*/
            content = @"{""data"":{""api_token"":""d076ea7ba6c8e705b921c6dcaeca7e07"",""jquery_date_format"":""d.m.Y"",""jquery_timeofday_format"":""H:i"",""date_format"":""%d.%m.%Y"",""default_workspace_id"":123323,""updated_at"":""2012-02-27T00:58:55+01:00"",""fullname"":""keydon@gmail.com"",""language"":""en_US"",""id"":172321,""time_entry_retention_days"":14,""beginning_of_week"":1,""new_time_entries_start_automatically"":true,""store_start_and_stop_time"":true,""email"":""keydon@gmail.com"",""timeofday_format"":""%H:%M""},""related_data_updated_at"":""2012-03-01T20:48:53+01:00""}";
            
            return JObject.Parse(content);
        }
    }
}
